package com.ocean.greedy.easy;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/array-partition/
 * <p>
 * 数组拆分
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/02/20 <br>
 */
public class ArrayPartition {

    public static void main(String[] args) {
        ArrayPartition arrayPartition = new ArrayPartition();
        int[] nums = {1, 4, 3, 2};
        int[] nums2 = {6, 2, 6, 5, 1, 2};
        int res = arrayPartition.arrayPairSum(nums);
        System.out.println(res);
        res = arrayPartition.arrayPairSum(nums2);
        System.out.println(res);
    }

    public int arrayPairSum(int[] nums) {
        // 排序
        Arrays.sort(nums);
        // 取下标为偶数的求和
        int n = nums.length;
        int res = 0;
        for (int i = 0; i < n; i = i + 2) {
            res += nums[i];
        }
        return res;
    }
}
